# 利用生成器，改写斐波那契数列
# 正常写：
def fbnq(num):
    a, b, i = 0, 1, 0
    print(a, end='')
    while i < num:
        print(', ', end=str(b))
        a, b = b, a+b
        i += 1

fbnq(8)
# 0, 1, 1, 2, 3, 5, 8, 13, 21

print()
# 生成器改写：
def tfbnq(num):
    a, b, i = 0, 1, 0
    yield a
    while i < num:
        yield b
        a, b = b, a + b
        i += 1
res = tfbnq(8)
print(res)
# <generator object tfbnq at 0x0000024863B928F0>
print(list(res))
# [0, 1, 1, 2, 3, 5, 8, 13, 21]

# 调用一次执行一次
def itfbnq():
    a, b = 0, 1
    yield a
    while True:
        yield b
        a, b = b, a + b

res = itfbnq()
print(next(res), end=', ')
print(next(res), end=', ')
print(next(res), end=', ')
print(next(res), end=', ')
print(next(res), end=', ')
print(next(res), end=', ')
print(next(res), end=', ')
print(next(res))
# 0, 1, 1, 2, 3, 5, 8, 13

